package com.yang.mall.helper;

import org.springframework.http.ResponseEntity;

import java.util.HashMap;

/**
 * Created by chijr on 17/12/13.
 */
public class ResponseHelper {


    public static int ResponseCode_Success = 200;


    public static int ResponseCode_AUTH_ERROR = 401;

    public static int ResponseCode_APP_ERROE = 550;

    //551,内部权限错误
    public static int ResponseCode_INTERNAL_PERMISSION_ERROR = 551;
    //552，参数格式错误
    public static int ResponseCode_PARAM_ERROR = 552;
    //553，对象没找到
    public static int ResponseCode_NOT_FOUNDED = 553;
    //554，普通错误
    public static int ResponseCode_COMMON_ERROR = 554;

    //555，网络请求失败
    public static int ResponseCode_REQUESTS_ERROR = 555;

    public static int ResponseCode_DB_ERROE = 560;


    private Object data;





    public static ResponseEntity success(){

        ResponseHelper object = new ResponseHelper(ResponseCode_Success,"success");

        return  ResponseEntity.status(ResponseCode_Success).body(object);

    }

    public static ResponseEntity error(){

        ResponseHelper object = new ResponseHelper(ResponseCode_APP_ERROE,"应用错误");
        return  ResponseEntity.status(ResponseCode_APP_ERROE).body(object);


    }


    public static ResponseEntity successWithPage(Object data, PageObject pageObject) {

        ResponseHelper object = new ResponseHelper(ResponseCode_Success, "success");
        HashMap<String, Object> m = new HashMap<String, Object>();
        m.put("list", data);
        m.put("page", pageObject);

        object.setData(m);

        return  ResponseEntity.status(ResponseCode_Success).body(object);

    }

    public static ResponseEntity success(Object data){
        ResponseHelper object = new ResponseHelper(ResponseCode_Success,"success");
        object.setData(data);
        return  ResponseEntity.status(ResponseCode_Success).body(object);
    }

    public static ResponseEntity error(String message){

        ResponseHelper object = new ResponseHelper(ResponseCode_APP_ERROE,message);

        return  ResponseEntity.status(ResponseCode_APP_ERROE).body(object);

    }

    public static ResponseEntity error(int code, String message){


        ResponseHelper object = new ResponseHelper(code,message);

        return  ResponseEntity.status(code).body(object);


    }




    public static ResponseEntity error(Exception e){

        ResponseHelper object = new ResponseHelper(ResponseCode_APP_ERROE,e.getMessage());


        return  ResponseEntity.status(ResponseCode_APP_ERROE).body(object);



    }



//    public static ResponseHelper error(int code,String message,Exception e){
//
//        ResponseHelper object = new ResponseHelper(code,message);
//
//
//        return object;
//
//    }

    public static ResponseEntity success(String key, Object value){

        ResponseHelper object = new ResponseHelper(ResponseCode_Success,"success");
        HashMap<String,Object> data = new HashMap<String, Object>();
        data.put(key,value);

        object.setData(data);
        return  ResponseEntity.status(ResponseCode_APP_ERROE).body(object);

    }


    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public ResponseHelper(int status, String message){

        this.status = status;
        this.message = message;

        this.data = new HashMap<String, Object>();
    }


    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    private String message;

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    private  int status;




}
